var arrayNesting = function(nums) {
    let res = 0;
    for(let i=0; i<nums.length; i++) {
        if(nums[i] !== -1) {
            let j = i, s = 0;
            while(nums[j] !== -1) {
                s++;
                let next = nums[j]
                nums[j] = -1;
                j = next;
            }
            res = Math.max(res, s)
        }    
    }
    return res;
};